package xyz.jcat.sat.oauth2.server.client;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import xyz.jcat.common.util.CollectionUtils;

import java.util.List;

@Repository
public class Oauth2ClientDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    private static final String SQL_QUERY_OAUTH2_CLIENT = "SELECT * FROM oauth2_client WHERE client_id = ?";

    public Oauth2Client getOauth2Client(String clientId) {
        List<Oauth2Client> list = jdbcTemplate.query(
                SQL_QUERY_OAUTH2_CLIENT,
                new BeanPropertyRowMapper<>(Oauth2Client.class),
                clientId);
        if(CollectionUtils.isNotEmpty(list)) {
            return list.get(0);
        }
        return null;
    }

}
